import java.awt.BorderLayout;

import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class PlayerGUI extends JFrame 
{
	
	private static Player player;
	
	private PlayerButtonPanel buttonPanel  = new PlayerButtonPanel(this);
	private static JTextArea messages = new JTextArea();
	private static JTextArea keyInfoArea = new JTextArea();
	private static JTextField playerInput = new JTextField();
	

	public PlayerGUI(Player player)
	{
	
		setLayout(new BorderLayout());
		add(new JScrollPane(messages), BorderLayout.CENTER);
		messages.setEditable(false);
		add(keyInfoArea, BorderLayout.WEST);
		keyInfoArea.setText(player.toString());
		keyInfoArea.setEditable(false);
		add(playerInput,BorderLayout.SOUTH);
		playerInput.setEditable(true);
		add(buttonPanel, BorderLayout.EAST);
		setTitle("player");
		setSize(700,400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	/**
	 * Updates current player to a new player
	 * @param player
	 */
	public static void updatePlayer(Player player)
	{
		PlayerGUI.player = player;
		keyInfoArea.setText(player.toString());
	}
	
	public static boolean isTurn() throws IOException, InterruptedException
	{
		player.sendMessage("hello");
		Thread.sleep(200);
		if (player.getMessage().equals("not your turn"))
		{return false;}
		else
		{return true;}
		
	}
	
	public static void addMessage(String message)
	{
		messages.append(message + "\n");
	}
	/**
	 * Performs an attack, sends if target is inputed
	 * @throws IOException 
	 * @throws NumberFormatException 
	 * @throws InterruptedException 
	 */
	public void attack() throws NumberFormatException, IOException, InterruptedException
	{
		if(isTurn())
		{
			if(!playerInput.getText().equals(""))
			{
				int target = Integer.parseInt(playerInput.getText());
				try 
				{player.sendAttack(target);} 
				catch (Exception e)
				{
					PlayerGUI.messages.append("attack failed \n");
					e.printStackTrace();
				}
				messages.append("sent attack \n");
				playerInput.setText("");
			}
			else
			{
				Attack attack = player.attack();
				if(attack != null){messages.append("attack: " + player.attack().toString() + "\n");}
				else{messages.append("attack failed \n");}
				keyInfoArea.setText(player.toString());
			}
		}
	}
		
	/**
	 * connects to DM (server)
	 */
	public static void connect()
	{
		try
		{
			if(!playerInput.getText().equals(""))
			{
				String input = playerInput.getText();
				String[] sections = input.split(" ");
				String ip = sections[0];
				int port = Integer.parseInt(sections[1]);
				player.connect(ip, port);
				playerInput.setText("");
			}
			else
			{
				player.connect("127.0.0.1", 8000);
			
			}
			Thread.sleep(200);
			addMessage(player.getMessage());
		}
		catch(Exception e)
		{
			System.err.println("error connecting");
			e.printStackTrace();
		}
	}
	
	public static void heal()
	{
		try {player.heal(Integer.parseInt(playerInput.getText()));}
		catch(Exception e){messages.append("enter amount to heal\n");}

		playerInput.setText("");
		keyInfoArea.setText(player.toString());
	}
	
	public static void move() throws IOException, InterruptedException
	{
		if(isTurn())
		{
			try{player.move(Integer.parseInt(playerInput.getText()));}
			catch(Exception e){messages.append("enter distance moved\n");}
		
			playerInput.setText("");
			keyInfoArea.setText(player.toString());
		}
	}
		
	
	public static void sendWait() throws IOException
	{
		player.sendMessage("wait");
		messages.append("sent wait/n");
	}
	
	public static void update()
	{
		keyInfoArea.setText(player.toString());
	}
	
	public void endTurn()
	{
		player.endTurn();
		keyInfoArea.setText(player.toString());
	}
	
	public static void loadPlayer()
	{
		new PlayerLoadPanel(player);
	}
	
	public static void loadWeapon()
	{
		if(!playerInput.getText().equals(""))
		{
			try 
			{
				player.setWeapon(Database.loadWeapon(playerInput.getText()));
				messages.append("loaded Weapon from database\n");
			}
			catch (SQLException e) 
			{
				e.printStackTrace();
				messages.append("error loading Weapon\n");
			}
			keyInfoArea.setText(player.toString());
		}
		else
		{
			messages.append("enter weapon name\n");
		}
		playerInput.setText("");
	}
	
	public static void loadArmor()
	{
		if(!playerInput.getText().equals(""))
		{
			try 
			{
				player.setArmor(Database.loadArmor(playerInput.getText()));
				messages.append("loaded Armor from database\n");
			}
			catch (SQLException e) 
			{
				e.printStackTrace();
				messages.append("error loading Armor\n");
			}
			keyInfoArea.setText(player.toString());
		}
		else
		{
			messages.append("enter armor name\n");
		}
		playerInput.setText("");
	}
	
	public static void main(String[] args)
	{
		player = new Player();
		new PlayerGUI(player);
	}
}
